package com.dongdongshop.common;

import com.aliyuncs.exceptions.ClientException;
import com.dongdongshop.service.SmsService;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.http.HttpResponse;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
@RocketMQMessageListener(topic = "send-sms-code",consumerGroup = "my-consumer-send-sms")
public class SendSmsCode implements RocketMQListener<String> {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SmsService smsService;
    @Value("${redisSms.key}")
    private String key;

    @Override
    public void onMessage(String s) {
        String code = RandomStringUtils.randomNumeric(4);
        HashOperations hashOperations = redisTemplate.opsForHash();
        hashOperations.put(key, s,code);
        String accessKeyId = "LTAI4GG4mWWJ28D7QkD8F9LL";
        String accessSecret = "aEsWaIpvz3OYKDZefxtzwDIv5JIiRd";
        java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        df.setTimeZone(new java.util.SimpleTimeZone(0, "GMT"));// 这里一定要设置GMT时区
        java.util.Map<String, String> paras = new java.util.HashMap<String, String>();
        // 1. 系统参数
        paras.put("SignatureMethod", "HMAC-SHA1");
        paras.put("SignatureNonce", java.util.UUID.randomUUID().toString());
        paras.put("AccessKeyId", accessKeyId);
        paras.put("SignatureVersion", "1.0");
        paras.put("Timestamp", df.format(new java.util.Date()));
        paras.put("Format", "XML");
        // 2. 业务API参数
        paras.put("Action", "SendSms");
        paras.put("Version", "2017-05-25");
        paras.put("RegionId", "cn-hangzhou");
        paras.put("PhoneNumbers", s);
        paras.put("SignName", "srz商城");
        paras.put("TemplateParam", "{\"code\":"+code+"}");
        paras.put("TemplateCode", "SMS_194061663");
        paras.put("OutId", "123");
        // 3. 去除签名关键字Key
        if (paras.containsKey("Signature"))
            paras.remove("Signature");
        // 4. 参数KEY排序
        java.util.TreeMap<String, String> sortParas = new java.util.TreeMap<String, String>();
        sortParas.putAll(paras);
        // 5. 构造待签名的字符串
        java.util.Iterator<String> it = sortParas.keySet().iterator();
        StringBuilder sortQueryStringTmp = new StringBuilder();
        while (it.hasNext()) {
            String key = it.next();
            try {
                sortQueryStringTmp.append("&").append(specialUrlEncode(key)).append("=").append(specialUrlEncode(paras.get(key)));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        String sortedQueryString = sortQueryStringTmp.substring(1);// 去除第一个多余的&符号
        StringBuilder stringToSign = new StringBuilder();
        stringToSign.append("GET").append("&");
        try {
            stringToSign.append(specialUrlEncode("/")).append("&");
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            stringToSign.append(specialUrlEncode(sortedQueryString));
        } catch (Exception e) {
            e.printStackTrace();
        }
        String sign = null;
        try {
            sign = sign(accessSecret + "&", stringToSign.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 6. 签名最后也要做特殊URL编码
        String signature = null;
        try {
            signature = specialUrlEncode(sign);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(paras.get("SignatureNonce"));
        System.out.println("\r\n=========\r\n");
        System.out.println(paras.get("Timestamp"));
        System.out.println("\r\n=========\r\n");
        System.out.println(sortedQueryString);
        System.out.println("\r\n=========\r\n");
        System.out.println(stringToSign.toString());
        System.out.println("\r\n=========\r\n");
        System.out.println(sign);
        System.out.println("\r\n=========\r\n");
        System.out.println(signature);
        System.out.println("\r\n=========\r\n");
        // 最终打印出合法GET请求的URL
        System.out.println("http://dysmsapi.aliyuncs.com/?Signature=" + signature + sortQueryStringTmp);
        HttpUtils.doGet("http://dysmsapi.aliyuncs.com/?Signature=" + signature + sortQueryStringTmp);
    }
    public static String specialUrlEncode(String value) throws Exception {
        return java.net.URLEncoder.encode(value, "UTF-8").replace("+", "%20").replace("*", "%2A").replace("%7E", "~");
    }
    public static String sign(String accessSecret, String stringToSign) throws Exception {
        javax.crypto.Mac mac = javax.crypto.Mac.getInstance("HmacSHA1");
        mac.init(new javax.crypto.spec.SecretKeySpec(accessSecret.getBytes("UTF-8"), "HmacSHA1"));
        byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
        return new sun.misc.BASE64Encoder().encode(signData);
    }
   /* @Override
    public void onMessage(String phone) {
        String code = RandomStringUtils.randomNumeric(4);
        HashOperations hashOperations = redisTemplate.opsForHash();
        hashOperations.put(key, phone,code);
        try {
            smsService.sendSms(phone,code);
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }*/
}
